package com.lanou.zhihu.web.controller;

import com.lanou.zhihu.entity.Comment;
import com.lanou.zhihu.entity.User;
import com.lanou.zhihu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/10/26.
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @ResponseBody
    @RequestMapping("/add")
    public boolean add(Comment comment, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        comment.setUid(user.getId());
        return commentService.add(comment);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public boolean delete(Integer id, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        return commentService.delete(user.getId(), id);
    }

    @ResponseBody
    @RequestMapping("/findByAid")
    public Map<String, Object> findByAid(Integer aid) {
        Map<String, Object> map = new HashMap<>();
        List<Comment> commentList = commentService.findByAid(aid);
        map.put("rows", commentList);
        return map;
    }

    @ResponseBody
    @RequestMapping("/getCountByAid")
    public Integer getCountByAid(Integer aid) {
        Integer count = commentService.getCountByAid(aid);
        return count;
    }
}
